﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AllenalaMente.Games.Selection
{
    public partial class Item : UserControl
    {
        public int type = 0;
        public Point position;
        public bool isSelected = false;

        public bool isFixed;
        public bool isBeingDragged;
        private Point mouseOffset;
        public Point finalPosition;

        public Item(int t, Point p)
        {
            InitializeComponent();

            this.type = t;
            this.position = p;

            switch (t)
            { 
                case 0:
                    rectangleItem.Fill = new SolidColorBrush(Colors.Red);
                    break;

                case 1:
                    rectangleItem.Fill = new SolidColorBrush(Colors.Blue);
                    break;
            }

            isFixed = false;
            isBeingDragged = false;
            mouseOffset = new Point(0, 0);

            MouseLeftButtonDown += Item_MouseLeftButtonDown;
            MouseLeftButtonUp += Item_MouseLeftButtonUp;
        }

        #region EVENTI

        /// <summary>
        /// Al clic sinistro inizia il trascinamento
        /// </summary>
        private void Item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Se il pezzo è già stato fissato esci
            if (isFixed || isSelected)
                return;

            // Altrimenti inizia il trascinamento
            isBeingDragged = true;
            mouseOffset = e.GetPosition(this);

            e.Handled = true;
        }

        /// <summary>
        /// Al rilascio del pulsante sinistro controllo se il pezzo è nella giusta posizione
        /// </summary>
        private void Item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isFixed || isSelected) return;

            mouseOffset = new Point(0, 0);

            Point currentPosition = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));

            // Se il pezzo si trova in un intorno della posizione corretta, allora lo fisso
            if (currentPosition.X < 200)
            {
                isFixed = true;
                Canvas.SetLeft(this, 25);
                //Canvas.SetTop(this, finalPosition.Y);
                Canvas.SetZIndex(this, 0);
                MouseLeftButtonDown -= Item_MouseLeftButtonDown;
                MouseLeftButtonUp -= Item_MouseLeftButtonUp;
            }

            // Se il pezzo si trova in un intorno della posizione corretta, allora lo fisso
            if (currentPosition.X > 600)
            {
                isFixed = true;
                Canvas.SetLeft(this, 665);
                //Canvas.SetTop(this, finalPosition.Y);
                Canvas.SetZIndex(this, 0);
                MouseLeftButtonDown -= Item_MouseLeftButtonDown;
                MouseLeftButtonUp -= Item_MouseLeftButtonUp;
            }
        }

        #endregion

        #region Metodi

        public bool Intersect(Rectangle rect)
        {
            Rect r1 = new Rect(Canvas.GetLeft(rect), Canvas.GetTop(rect), rect.Width, rect.Height);

            return r1.Contains(new Point(position.X + rectangleItem.Width / 2, position.Y + rectangleItem.Height / 2));
        }

        /// <summary>
        /// Aggiorna la posizione del pezzo in base a quella del mouse nel contenitore
        /// </summary>
        /// <param name="mousePosition">La posizione del mouse relativa al contenitore</param>
        public void UpdatePosition(Point mousePosition)
        {
            var newPosition = new Point();
            newPosition.X = mousePosition.X - mouseOffset.X;
            newPosition.Y = mousePosition.Y - mouseOffset.Y;
            Canvas.SetLeft(this, newPosition.X);
            Canvas.SetTop(this, newPosition.Y);

            position = newPosition;
            textBlock1.Text = position.ToString();
        }

        #endregion
    }
}
